import api from '@/api';

export default {
  namespace: 'credit',

  state: {
    fixedList: {
      list: [],
      pagination: {},
    },
    currentCredit: {},
    currentNewCredit: {},
    detailList: {
      list: [],
      pagination: {},
    },
    detailTransList: {
      list: [],
      pagination: {},
    },
    auditList: {
      list: [],
      pagination: {},
    },
    currentTemporary: {},
    billDate: [],
    creditSelectedCus: {
      cus: [],
      status: '',
    },
    fixedSelectedCus: {
      cus: [],
      status: '',
    },
    tempSelectedCus: {
      cus: [],
      status: '',
    },
    auditSelectedCus: {
      cus: [],
      status: '',
    },
  },

  effects: {
    *fetchCredits({ payload }, { call, put }) {
      const response = yield call(api.credit.queryCredits, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveFixedList',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },
    *fetchNewCurrent({ payload }, { call, put }) {
      const response = yield call(api.credit.queryApplyDetail, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveNewCurrent',
          payload: response.data,
        });
      }
      return response;
    },

    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.credit.queryCurrent, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: response.data,
        });
      }
      return response;
    },

    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(api.credit.queryDetail, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveDetail',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },
    *fetchBillDate({ payload }, { call, put }) {
      const response = yield call(api.credit.queryBillDate, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveBillDate',
          payload: response.data,
        });
      }
      return response;
    },
    *fetchDetailTransaction({ payload }, { call, put }) {
      const response = yield call(api.credit.queryDetailTransaction, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveDetailTransaction',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },
    *fetchAuditList({ payload }, { call, put }) {
      const response = yield call(api.credit.queryAuditList, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveAuditList',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },

    *add({ payload }, { call }) {
      return yield call(api.credit.creat, payload);
    },
    *lock({ payload }, { call }) {
      return yield call(api.credit.lock, payload);
    },
    *unlock({ payload }, { call }) {
      return yield call(api.credit.unlock, payload);
    },

    *submitAudit({ payload }, { call }) {
      return yield call(api.credit.audit, payload);
    },
  },

  reducers: {
    saveFixedList(state, { payload }) {
      return {
        ...state,
        fixedList: payload,
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentCredit: payload,
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        currentCredit: {},
      };
    },
    saveNewCurrent(state, { payload }) {
      return {
        ...state,
        currentNewCredit: payload,
      };
    },
    clearNewCurrent(state) {
      return {
        ...state,
        currentNewCredit: {},
      };
    },
    saveDetail(state, { payload }) {
      return {
        ...state,
        detailList: payload,
      };
    },
    saveDetailTransaction(state, { payload }) {
      return {
        ...state,
        detailTransList: payload,
      };
    },
    saveAuditList(state, { payload }) {
      return {
        ...state,
        auditList: payload,
      };
    },
    saveBillDate(state, { payload }) {
      return {
        ...state,
        billDate: payload,
      };
    },
    saveCreditSelectedCus(state, { payload }) {
      return {
        ...state,
        creditSelectedCus: payload,
      };
    },
    saveFixedSelectedCus(state, { payload }) {
      return {
        ...state,
        fixedSelectedCus: payload,
      };
    },
    saveTempSelectedCus(state, { payload }) {
      return {
        ...state,
        tempSelectedCus: payload,
      };
    },
    saveAuditSelectedCus(state, { payload }) {
      return {
        ...state,
        auditSelectedCus: payload,
      };
    },
  },
};
